package com.sanbi.common.util;

import com.sanbi.common.vo.Enums;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

public class FileUtil {
    /**
     * @desc 创建文件路径，格式: yyyy/MM/dd
     * @author wanggang
     * @time 2022/7/8 12:34
     */
    public static String createFilePath() {
        String strYear = DateTimeFormatter.ofPattern("yyyy").format(LocalDateTime.now());
        String strMonth = DateTimeFormatter.ofPattern("MM").format(LocalDateTime.now());
        String strDay = DateTimeFormatter.ofPattern("dd").format(LocalDateTime.now());

        return String.format("%s/%s/%s",strYear,strMonth,strDay);
    }

    /**
     * @desc 获取文件扩展名
     * @author wanggang
     * @time 2022/7/6 14:44
     */
    public static String getFileExtension(String fileName){
        int index=fileName.lastIndexOf('.');
        return index>0? fileName.substring(index+1):"";
    }

    /**
     * @desc 生成文件名，格式：yyyyMMddHHmmssxxxxxx.ext
     * @author wanggang
     * @time 2022/7/6 14:44
     */
    public static String createFileName(String fileName){
        String newName = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now())+StringUtil.getRandomString(6);
        String extension=getFileExtension(fileName);
        if(StringUtils.isNoneBlank(extension)){
            newName=String.format("%s.%s",newName,extension);
        }
        return newName;
    }

    //outFileNameType: 输出文件名的格式：1 随机，2 源文件名-时间戳，3 源文件名
    public static String createFileName(String originFileName,int outFileNameType){
        Enums.FilenameType filenameType= Enums.FilenameType.getItem(outFileNameType);
        String newName ="";
        String originName=getOriginFilename(originFileName);
        String extension=getFileExtension(originFileName);
        switch (filenameType){
            case Random:
            case Undefiled:
                newName = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now())+StringUtil.getRandomString(6);
                break;
            case OriginAndRandom:
            default:
                String timestamp=DateTimeFormatter.ofPattern("yyMMddHHmmssSSS").format(LocalDateTime.now());
                newName = String.format("%s-%s",originName,timestamp);
                break;
            case Origin:
                newName = originName;
                break;
        }

        if(StringUtils.isNoneBlank(extension)){
            newName=String.format("%s.%s",newName,extension);
        }

        return newName;
    }

    public static boolean isImageFile(String fileExtension){
        Set extSet=new HashSet();
        extSet.add("bmp");
        extSet.add("gif");
        extSet.add("jpeg");
        extSet.add("jpg");
        extSet.add("png");

        boolean isImageFile=extSet.contains(fileExtension)?true:false;

        return isImageFile;
    }

    public static boolean isImageFile(Enums.FileType fileType){
        return fileType.getType()=="image"?true:false;
    }

    /**
     * @desc 获取文件原名
     * @author wanggang
     * @time 2023/8/22 14:23
     */
    private static String getOriginFilename(String fileName){
        int index=fileName.lastIndexOf('.');
        return index>0? fileName.substring(0,index):fileName;
    }

    public static void main(String[] args) {
        boolean isImage=isImageFile(Enums.FileType.getItem("image/png"));
        System.out.println(isImage);
    }

}
